package com.jeecg.p3.system.order.web;

import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.base.BaseController;
import com.jeecg.p3.system.order.entity.ProductDetails;
import com.jeecg.p3.system.order.service.CourseOrderService;
import com.jeecg.p3.system.order.service.ProductDetailsService;
import com.jeecg.p3.system.poster.entity.AdvisorCoupon;
import com.jeecg.p3.system.poster.entity.CustomerCoupon;
import com.jeecg.p3.system.poster.service.AdvisorCouponService;
import com.jeecg.p3.system.poster.service.CustomerCouponService;
import com.jeecg.p3.system.vo.LoginUser;
import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 手动订单处理类
 */
@Controller
@RequestMapping("/system/back/order/manual")
public class ManualOrderController extends BaseController {
    @Autowired
    private CourseOrderService courseOrderService;
    @Autowired
    private ProductDetailsService productDetailsService;
    @Autowired
    private CustomerCouponService customerCouponService;
    @Autowired
    private AdvisorCouponService advisorCouponService;

    /**
     *
     *
     * @param request
     * @param response
     * @param pageNo 第几页
     * @param pageSize 页数大小
     * @throws Exception
     */
    @RequestMapping(value="/list.do",method = {RequestMethod.GET,RequestMethod.POST})
    public void findPage(HttpServletRequest request, HttpServletResponse response,
                         @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                         @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
        VelocityContext velocityContext = new VelocityContext();
        String viewName = "system/back/order/manual_order.vm";
        ViewVelocity.view(request,response,viewName,velocityContext);
    }

    /**
     * 手动开通订单
     * @param cusCouponId
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/manualOrder",method = {RequestMethod.GET,RequestMethod.POST})
    public AjaxJson manualOrder(String cusCouponId, HttpServletRequest request) {
        LoginUser user = getCurrentUser(request);
        AjaxJson j = new AjaxJson();
        CustomerCoupon customerCoupon = customerCouponService.findById(Long.valueOf(cusCouponId));
        AdvisorCoupon advisorCoupon = advisorCouponService.findById(Long.valueOf(customerCoupon.getAdvCouponId()));
        ProductDetails details = new ProductDetails();
        details.setCreateDate(DateUtil.dateToString(new Date()));
        details.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
        details.setCreateUser(user.getId());
        details.setStatus("1");
        details.setCouponId(cusCouponId);
        details.setProductId(advisorCoupon.getProductId());
        details.setOpenId(customerCoupon.getWxOpenId());
        details.setUserStatus(1);
        productDetailsService.insertPd(details);
        return j;
    }
}
